home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_2 / secant.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  949 b   |  37 lines  |  [MATF/MATL]

  1. function [p1,y1,err,P] = secant(f,p0,p1,delta,epsilon,max1)
  2. % [p1,y1,err] = secant(f,p0,p1,delta,epsilon,max1)
  3. % [p1,y1,err,P] = secant(f,p0,p1,delta,epsilon,max1)
  4. % The secant method is used to locate a root.
  5. % f is the function, input.
  6. % p0 is a starting point, input.
  7. % p1 is a starting point, input.
  8. % delta is the tolerance for p1, input.
  9. % epsilon is the tolerance for y1, input.
  10. % max1 is the maximum number of iterations, input.
  11. % p1 is the root, output.
  12. % y1 is the function value, output.
  13. % err is the error estimate for p1, output.
  14. % P is the is the vector of iterations, output.
  15. P(1) = p0;
  16. P(2) = p1;
  17. y0 = feval(f,p0);
  18. y1 = feval(f,p1);
  19. for k=1:max1,
  20.   df = (y1-y0)/(p1-p0);
  21.   if df == 0,
  22.     dp = 0;
  23.   else
  24.     dp = y1/df;
  25.   end
  26.   p2 = p1 - dp;
  27.   y2 = feval(f,p2);
  28.   err = abs(dp);
  29.   relerr = err/(abs(p2)+eps);
  30.   p0 = p1;
  31.   y0 = y1;
  32.   p1 = p2;
  33.   y1 = y2;
  34.   P = [P,p2];
  35.   if (err<delta)|(relerr<delta)|(abs(y2)<epsilon), break, end
  36. end
  37.